package com.solcoo.merchant.base;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.widget.BaseAdapter;

import com.solcoo.common.util.ArrayUtil;
import com.solcoo.common.util.CollectionUtil;

/**
 * @Description
 * 
 * @author panjunlin
 * 
 * @version 创建时间：2012-9-27 上午10:17:05
 * 
 */
public abstract class BaseListAdapter<T> extends BaseAdapter {

	protected List<T> mdata;

	protected Context context;

	protected boolean hasMore = true;

	public int currentPage = 1;

	public int pageSize = 10;

	public BaseListAdapter(Context context) {
		this(context, null);
	}

	public BaseListAdapter(Context context, List<T> mdata) {
		this.context = context;
		this.mdata = mdata == null ? new ArrayList<T>() : mdata;
	}

	@Override
	public int getCount() {
		return mdata.size();
	}

	@Override
	public T getItem(int position) {
		return CollectionUtil.get(mdata, position);
	}

	protected void clear() {
		this.mdata.clear();
	}

	public boolean add(ListResult<T> result) {
		if (result == null || result.isEmpty()) {
			return false;
		}
		this.mdata.addAll(ArrayUtil.asList(result.mdata));
		notifyDataSetChanged();
		return true;
	}

	public boolean update(ListResult<T> result) {
		if (result == null || result.isEmpty()) {
			return false;
		}
		this.mdata = ArrayUtil.asList(result.mdata);
		notifyDataSetChanged();
		return true;
	}

	//
	// protected void setHasMore(ListResult<T> result) {
	// if (result != null) {
	// hasMore = currentPage < result.totalPage;
	// }
	// }

	public boolean hasMore() {
		return hasMore;
	}

	public interface IResultWrapper {
		boolean isEmpty();
	}

	public static class ListResult<T> implements IResultWrapper {

		public T[] mdata; // 数据列表

		@Override
		public boolean isEmpty() {
			return ArrayUtil.isEmpty(mdata);
		}

	}

}
